package com.example.elemboot.Service.impl;

import com.example.elemboot.Dao.IBusiness;
import com.example.elemboot.Dao.ICart;
import com.example.elemboot.Dao.IFood;
import com.example.elemboot.Dao.IUser;
import com.example.elemboot.Entity.Business;
import com.example.elemboot.Entity.Cart;
import com.example.elemboot.Entity.Food;
import com.example.elemboot.Entity.User;
import com.example.elemboot.Service.ICartService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CartService implements ICartService {

    @Resource
    private ICart cartDao;
    @Resource
    private IFood foodDao;
    @Resource
    private IBusiness businessDao;

    @Resource
    private IUser userDao;
    @Override
    public Boolean saveCart(String phone, Integer businessId, Integer foodId, Integer quantity) {
        try {
            User user = userDao.findUserByUserPhone(phone);
            Business business = businessDao.findById(businessId).get();
            Food food = foodDao.findById(foodId).get();
            Cart cart = new Cart(quantity, food, business, user);
            cartDao.save(cart);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public Boolean updateCart(String phone, Integer businessId, Integer foodId, Integer quantity) {
        try {
            User user = userDao.findUserByUserPhone(phone);
            Business business = businessDao.findById(businessId).get();
            Food food = foodDao.findById(foodId).get();
            Cart cart = cartDao.findCartByBusinessAndFoodAndUser(business, food, user);
            if (cart == null){
                return false;
            }
            cart.setQuantity(quantity);
            cartDao.save(cart);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public Boolean removeCartItem(String phone, Integer businessId, Integer foodId, Integer quantity) {
        try {
            User user = userDao.findUserByUserPhone(phone);
            Business business = businessDao.findById(businessId).get();
            Food food = foodDao.findById(foodId).get();
            Cart cart = cartDao.findCartByBusinessAndFoodAndUser(business, food, user);
            if (cart == null){
                return false;
            }
            if (quantity<=0){
                //如果减到0就删除，否则更新
                cartDao.deleteCartByCartId(cart.getCartId());
                return true;
            }
            cart.setQuantity(quantity);
            cartDao.save(cart);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public List<Cart> getCartList(String userPhone, Integer businessId) {
        User user = userDao.findUserByUserPhone(userPhone);
        Business business = businessDao.findById(businessId).get();
        return cartDao.findCartsByBusinessAndUser(business, user);
    }


}
